﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Transactions;

public partial class AttendanceWebForm_LeaveData_Approve : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gv.DataBind();
        }
    }

    protected void gv_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "MyAgree":
                {
                    var row = (e.CommandSource as Control).NamingContainer as GridViewRow;

                    if (Approveata(row, true) == true)
                    {
                        gv.DataBind();
                    }
                }

                break;
            case "MyReject":
                {
                    var row = (e.CommandSource as Control).NamingContainer as GridViewRow;

                    if (Approveata(row, false) == true)
                    {
                        gv.DataBind();
                    }
                }

                break;
        }
    }

    protected bool Approveata(GridViewRow row, bool result)
    {
        using (TransactionScope scope = new TransactionScope())
        {
            var db = new DB("Attendance");

            db.ExecuteNonQuery("UPDATE LeaveData SET Status=@Status WHERE LeaveID = @LeaveID "
                , new DBParameter("LeaveID", gv.DataKeys[row.RowIndex]["LeaveID"]), new DBParameter("Status", result == true ? "Y" : "N"));

            scope.Complete();
        }

        return true;
    }
}